package kxfang.com.common.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import androidx.fragment.app.Fragment;

import com.gyf.immersionbar.ImmersionBar;

import kxfang.com.common.R;

/**
 * 状态栏适配类
 */
public class StatusBarUtil {
    public static void setTransparent(Activity activity) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
            return;
        }
        transparentStatusBar(activity);
        setRootView(activity);
    }

    /**
     * 使状态栏透明
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private static void transparentStatusBar(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //需要设置这个flag contentView才能延伸到状态栏
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            //状态栏覆盖在contentView上面，设置透明使contentView的背景透出来
            activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else {
            //让contentView延伸到状态栏并且设置状态栏颜色透明
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
    }

    /**
     * 设置根布局参数
     */
    private static void setRootView(Activity activity) {
        ViewGroup parent = (ViewGroup) activity.findViewById(android.R.id.content);
        for (int i = 0, count = parent.getChildCount(); i < count; i++) {
            View childView = parent.getChildAt(i);
            if (childView instanceof ViewGroup) {
                childView.setFitsSystemWindows(true);
                ((ViewGroup) childView).setClipToPadding(true);
            }
        }
    }

    /**
     * 全局适配顶部状态栏
     *
     * @param activity
     * @param view
     */
    public static void setTopActivityView(Activity activity, View view) {
        ImmersionBar.with(activity)
                .statusBarView(view)
                .statusBarColor(R.color.common_colorWhite)
                .autoDarkModeEnable(true) //自动状态栏字体和导航栏图标变色，必须指定状态栏颜色和导航栏颜色才可以自动变色哦
                .autoStatusBarDarkModeEnable(true, 0.2f) //自动状态栏字体变色，必须指定状态栏颜色才可以自动变色哦
                .init();

    }

    /**
     * 设置显示沉浸式图片和显示正常
     *
     * @param activity
     * @param view
     * @param color
     */
    public static void setTopActivityView(Activity activity, View view, int color) {
        ImmersionBar.with(activity)
                .statusBarView(view)
                .statusBarColor(color)
                .autoDarkModeEnable(true) //自动状态栏字体和导航栏图标变色，必须指定状态栏颜色和导航栏颜色才可以自动变色哦
                .autoStatusBarDarkModeEnable(true, 0.2f) //自动状态栏字体变色，必须指定状态栏颜色才可以自动变色哦
                .init();

    }

    /**
     * 设置白色字体状态栏
     *
     * @param activity
     * @param view
     * @param color
     */
    public static void setTopWhiteColorView(Activity activity, View view, int color) {
        ImmersionBar.with(activity)
                .statusBarView(view)
                .statusBarColor(color)
                .autoDarkModeEnable(false) //自动状态栏字体和导航栏图标变色，必须指定状态栏颜色和导航栏颜色才可以自动变色哦
                .autoStatusBarDarkModeEnable(false, 1f) //自动状态栏字体变色，必须指定状态栏颜色才可以自动变色哦
                .init();
    }

    /**
     * fragment沉浸式适配
     *
     * @param activity
     * @param view
     */
    public static void setTopFragmentView(Fragment activity, View view, int color) {
        ImmersionBar.with(activity)
                .statusBarView(view)
                .statusBarColor(color)
                .statusBarDarkFont(true)
//                .autoDarkModeEnable(false) //自动状态栏字体和导航栏图标变色，必须指定状态栏颜色和导航栏颜色才可以自动变色哦
                .autoStatusBarDarkModeEnable(true, 0.2f) //自动状态栏字体变色，必须指定状态栏颜色才可以自动变色哦
                .init();

    }
}
